package CrawlerbyCategories;
/**
 * Created by Administrator on 2016/12/26 0026.
 */

import DataBase.DB;
import DownloadVideo.DownloadVideo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.sql.ResultSet;
import java.sql.Statement;

import static org.apache.commons.lang3.StringEscapeUtils.unescapeJava;

public class categoriesCarwler {
    public static int n=0,t=187;
    static String content="c";

    public static void Insert(String str,char[][] av)
    {
        if(content.contains("empty!"))
            return;

        int j,k,l=0;
        for(int i=0;i>=0;)
        {
            i=content.indexOf(str,i+1);
            if(i<0)
                return;
            else
            {
                j=content.indexOf(':', i);
                k=content.indexOf(',', i);
                switch(str)
                {
                    case "\"aid\"":
                        av[l]=new char[k-j-1];
                        content.getChars(j+1,k,av[l],0);break;
                    case "\"author\"":
                        av[l]=new char[k-j-3];
                        content.getChars(j+2,k-1,av[l],0);break;
                    case "tname":
                        av[l]=new char[k-j-3];
                        content.getChars(j+2,k-1,av[l],0);break;
                    case "coin":
                        av[l]=new char[k-j-1];
                        content.getChars(j+1,k,av[l],0);break;
                    case "favorites":
                        av[l]=new char[k-j-1];
                        content.getChars(j+1,k,av[l],0);break;
                    case "title":
                        av[l]=new char[k-j-3];
                        content.getChars(j+2,k-1,av[l],0);break;
                    case "play":
                        av[l]=new char[k-j-1];
                        content.getChars(j+1,k,av[l],0);break;
                }
                l++;
            }
        }
    }

    public static void cra_categories() throws Exception
    {
        DB.db();
        while(!content.contains("empty!"))
        {
            char[][] avnumber=new char[20][];
            char[][] author=new char[20][];
            char[][] category=new char[20][];
            char[][] NumberOfCoins=new char[20][];
            char[][] NumberOfFavorites=new char[20][];
            char[][] title=new char[20][];
            char[][] view=new char[20][];
            String str1="http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=?&type=jsonp&tid=153&pn="+t+"&";
            t++;
            HttpClient httpClient=HttpClients.createDefault();
            HttpGet req=new HttpGet(str1);
            req.addHeader("Accept","application/json,text/javascript,*/*;q=0.01");
            req.addHeader("Accept-Encoding","gzip,deflate,sdch");
            req.addHeader("Accept_language","zh-CN,zh;q=0.8");
            req.addHeader("Content-Type","text/html;charset=UTF-8");
            req.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
            HttpResponse rep=httpClient.execute(req);
            HttpEntity repEntity=rep.getEntity();
            content=EntityUtils.toString(repEntity);
            System.out.println(content);

            Insert("\"aid\"",avnumber);
            Insert("\"author\"",author);
            Insert("tname",category);
            Insert("coin",NumberOfCoins);
            Insert("favorites",NumberOfFavorites);
            Insert("title",title);
            Insert("play",view);
            System.out.println("succeed!");
            //String a=unescapeJava(String.valueOf(title[1]));
            for(int i=0;i<20;i++)
                if(avnumber[i]!=null)
                {
                    String av_url="http://www.ibilibili.com/video/av"+String.valueOf(avnumber[i]);
                   try {
                       DB.insert_av(Integer.parseInt(String.valueOf(avnumber[i])),
                               av_url,
                               unescapeJava(String.valueOf(title[i])),
                               unescapeJava(String.valueOf(author[i])),
                               unescapeJava(String.valueOf(category[i])),
                               Integer.parseInt(String.valueOf(NumberOfCoins[i])),
                               Integer.parseInt(String.valueOf(NumberOfFavorites[i])),
                               Integer.parseInt(String.valueOf(view[i])));
                   }
                   catch (Exception e){
                       e.printStackTrace();
                   }
                }
            System.out.println(t);
        }

        String query = "select * from av_html order by favorite*1 desc limit 0,3;";
        Statement sql_statement= DB.con.createStatement();
        ResultSet result = sql_statement.executeQuery(query);

        while(result.next())
        {
            String av_url="http://www.ibilibili.com/video/av"+result.getString("av_id")+"/";
            System.out.println(av_url);


            Document doc = Jsoup.connect(av_url).get();
            Elements links=doc.select("a[href]");
            String down_url=null;
            System.out.println("Downloading now!");
            for(Element link :links)
            {
                int k=0;
                if(link.ownText().startsWith("视频下载"))
                {
                    down_url = link.attr("href");
                    String FileName=result.getString("av_id")+k+".mp4";k++;
                    String FilePath="F:\\video\\"+FileName;
                    DownloadVideo.test(down_url,FilePath);
                    System.out.println(down_url);
                }
            }
        }
        System.out.println("database");
        DB.close();
    }

}